package com.zghhost.stockweb.commandline;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zghhost.stockweb.pojo.GeGuYingyebuEntity;
import com.zghhost.stockweb.pojo.SNameEntity;
import com.zghhost.stockweb.pojo.WangyiYidongBean;
import com.zghhost.stockweb.util.HttpUtil;
import com.zghhost.stockweb.util.NumberUtil;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/5/17 13:51
 */
public class GeguYidong {
    private static final String WANGYI_YIDONG_URL = "http://quotes.money.163.com/hs/realtimedata/service/radar.php?host=/hs/realtimedata/service/radar.php&page=0&fields=CODE,NAME,PRICE,PERCENT,DATE,TYPES,SYMBOL,NUMBER,HSL&sort=DATE&order=desc&count=1000000&type=query&callback=callback_712532230&req=51348";

    //[大买单 , 大买单 跌停打开 , 竞价放量 , 快速下跌 , 涨停 , 大卖单 竞价放量 , 跌停 , 快速上涨 , 大卖单 , 跌停打开 , 大卖单 跌停 , 大买单 涨停 , 大买单 竞价放量 , 大卖单 涨停打开 , 涨停打开 ]
    public static void main(String[] args)throws Exception{
        Set<String> titleDist = new HashSet<>();
        Set<String> typeDist = new HashSet<>();

        JSONArray jarr = fetchYiDong();

        List<WangyiYidongBean> yidongs = JSONArray.parseArray(jarr.toString(),WangyiYidongBean.class);


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        File dfile = new File("/Users/wow/Downloads/"+sdf.format(new Date())+"_DADAN.xls");
        WritableWorkbook wb = Workbook.createWorkbook(new FileOutputStream(dfile));
        WritableSheet ws = wb.createSheet("sheet",0);

        int row=0,col=0;

        ws.addCell(new Label(col++,row,"代码"));
        ws.addCell(new Label(col++,row,"名称"));
        ws.addCell(new Label(col++,row,"时间"));

        ws.addCell(new Label(col++,row,"发生量"));
        ws.addCell(new Label(col++,row,"JIAGE"));
        ws.addCell(new Label(col++,row,"金额"));
        ws.addCell(new Label(col++,row,"金额S"));
        ws.addCell(new Label(col++,row,"TYPE"));
        ws.addCell(new Label(col++,row,"HSL"));
        ws.addCell(new Label(col++,row,"INFO"));
        ws.addCell(new Label(col++,row,"TYPES"));
        ws.addCell(new Label(col++,row,"CODE"));
        ws.addCell(new Label(col++,row,"PERCENT"));
        ws.addCell(new Label(col++,row,"TITLE"));
        ws.addCell(new Label(col++,row,"RN"));

        col=0;
        row++;

        for(WangyiYidongBean yidong : yidongs){

            titleDist.add(yidong.getTITLE());
            typeDist.add(yidong.getTYPE());

            ws.addCell(new Label(col++,row,yidong.getSYMBOL()));
            ws.addCell(new Label(col++,row,yidong.getNAME()));
            ws.addCell(new Label(col++,row,yidong.getDATE()));

            ws.addCell(new Label(col++,row,String.valueOf(yidong.getNUMBER())));
            ws.addCell(new Label(col++,row,String.valueOf(yidong.getPRICE())));
            if(yidong.getTYPE().indexOf("卖") != -1){
                ws.addCell(new Label(col++,row,String.valueOf(0-yidong.getAMOUNT())));
                ws.addCell(new Label(col++,row,String.valueOf("-"+NumberUtil.formatNum(""+yidong.getAMOUNT(),false))));
            }else{
                ws.addCell(new Label(col++,row,String.valueOf(yidong.getAMOUNT())));
                ws.addCell(new Label(col++,row,String.valueOf(NumberUtil.formatNum(""+yidong.getAMOUNT(),false))));
            }
            ws.addCell(new Label(col++,row,String.valueOf(yidong.getTYPE())));
            ws.addCell(new Label(col++,row,String.valueOf(yidong.getHSL())));
            ws.addCell(new Label(col++,row,String.valueOf(yidong.getINFO())));
            ws.addCell(new Label(col++,row,String.valueOf(yidong.getTYPES())));
            ws.addCell(new Label(col++,row,String.valueOf(yidong.getCODE())));
            ws.addCell(new Label(col++,row,String.valueOf(yidong.getPERCENT())));
            ws.addCell(new Label(col++,row,String.valueOf(yidong.getTITLE())));
            ws.addCell(new Label(col++,row,String.valueOf(yidong.getRN())));

            col = 0;
            row++;
        }

        wb.write();
        wb.close();
    }

    public static JSONArray fetchYiDong()throws Exception{
        String resdata = HttpUtil.doHttpGet(WANGYI_YIDONG_URL,"utf-8");
        resdata = resdata.substring(resdata.indexOf("(")+1,resdata.length()-1);
        JSONObject job = JSONObject.parseObject(resdata);
        JSONArray jarr = job.getJSONArray("list");
        return jarr;
    }
}
